
package com.contractproductpricemodule.dao;
import com.contractproductpricemodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbContractProductPriceDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbContractProductPrice tbContractProductPrice=null;
    private List<TbContractProductPrice> tbContractProductPriceList=null;

    public boolean add(TbContractProductPrice inTbContractProductPrice){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_ContractProductPrice(ProductPriceMiddleId,ContractCode) values(?,?)";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbContractProductPrice.getProductPriceMiddleId());
        pt.setString(2,inTbContractProductPrice.getContractCode());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inContractProductPriceId){
        return UtilDAO.del("Tb_ContractProductPrice", "ContractProductPriceId", inContractProductPriceId);
    }
    public boolean edit(TbContractProductPrice inTbContractProductPrice){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_ContractProductPrice set ProductPriceMiddleId=?,ContractCode=? where ContractProductPriceId=?";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbContractProductPrice.getProductPriceMiddleId());
        pt.setString(2,inTbContractProductPrice.getContractCode());
        pt.setInt(3,inTbContractProductPrice.getContractProductPriceId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbContractProductPrice getTbContractProductPriceByContractProductPriceId(int inContractProductPriceId) {
        tbContractProductPrice=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ContractProductPrice where ContractProductPriceId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inContractProductPriceId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbContractProductPrice == null)tbContractProductPrice = getTbContractProductPrice(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbContractProductPrice;
    }
    public List<TbContractProductPrice> getTbContractProductPriceAll() {
        tbContractProductPriceList=new ArrayList<TbContractProductPrice>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ContractProductPrice";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractProductPriceList.add(getTbContractProductPrice(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbContractProductPriceList;
    }
    public List<TbContractProductPrice> getTbContractProductPriceByProductPriceMiddleId(int inProductPriceMiddleId) {
        tbContractProductPriceList=new ArrayList<TbContractProductPrice>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ContractProductPrice where ProductPriceMiddleId=?";
            pt.setInt(1, inProductPriceMiddleId);
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractProductPriceList.add(getTbContractProductPrice(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbContractProductPriceList;
    }
    public List<TbContractProductPrice> getTbContractProductPriceByContractCode(String inContractCode) {
        tbContractProductPriceList=new ArrayList<TbContractProductPrice>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ContractProductPrice where ContractCode=?";
            pt.setString(1, inContractCode);
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractProductPriceList.add(getTbContractProductPrice(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbContractProductPriceList;
    }
    public List<TbContractProductPrice> getTbContractProductPriceAll(VskPage vp) {
        tbContractProductPriceList=new ArrayList<TbContractProductPrice>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ContractProductPrice";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_ContractProductPrice", "ContractProductPriceId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractProductPriceList.add(getTbContractProductPrice(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbContractProductPriceList;
    }
    public TbContractProductPrice getTbContractProductPrice(ResultSet rs) throws Exception{
        TbContractProductPrice tbContractProductPrice = new TbContractProductPrice();
        tbContractProductPrice.setContractProductPriceId(rs.getInt("ContractProductPriceId"));
        tbContractProductPrice.setProductPriceMiddleId(rs.getInt("ProductPriceMiddleId"));
        tbContractProductPrice.setContractCode(rs.getString("ContractCode"));
        return tbContractProductPrice;
    }
}
